﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QuanLyGiaoVuUI.Models.RssFeeds;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace QuanLyGiaoVuUI.Models.RssFeeds
{
    public class RssReader
    {
        private string _URL="http://forum.uit.edu.vn/external.php?type=RSS2";
        public RssReader()
        {
            _URL = "http://forum.uit.edu.vn/external.php?type=RSS2";
        }
        public RssReader(String _url)
        {
            _URL = _url;
        }
        public IEnumerable<RssFeeds> GetRssFeed()
        {
            try
            {
                XDocument feedXml = XDocument.Load(_URL);
                var feeds = from feed in feedXml.Descendants("item")
                            select new RssFeeds
                            {
                                Title = feed.Element("title").Value,
                                Link = feed.Element("link").Value,
                                Description = Regex.Match(feed.Element("description").Value, @"^.{1,180}\b(?<!\s)").Value,
                                PubDate=DateTime.Parse(feed.Element("pubDate").Value.ToString())
                            };
                return feeds;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public void setURL(String _url)
        {
            _URL=_url;
        }
    }
}